home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
COMMUNIC
/
0132.ZIP
/
CVTBIN.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1985-04-28
|
6KB
|
226 lines
{ Program: CISBin
Programmer: Ray L. McVay
Started: 6 Dec, 1984
This is a Turbo Pascal program that converts a CompuServe
ASCII binary file to a PCDOS binary file and vice-versa. The
format used by CIS is based on the Intel HEX file format with
the exception of the final record type being that of a DATA record
rather than the End of File record type specified by Intel. The
following is a representative record:
:1800A8002C0083FB00750D8BDA4B8EC326031E030083C3028BD34A8E4B
3333 3333 3@AD checksum
3333 333@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAD data
3333 3@AD record type (00 = data, 01 = (Intel) last record)
333@MMAD load address
3@AD block size (number of data elements in block)
@D start of block character
The CIS2bin procedure is very simple minded in that it discards any
line that doesn't begin with the start of block character ':', and it
discards the Address and Record Type fields.
}
program CISBin;
CONST
VERSION = '1.0';
UPDATED = '9 Dec, 1984';
CBVERSION = '1.0';
CBUPDATED = '9 Dec, 1984';
BCVERSION = '1.0';
BCUPDATED = '9 Dec, 1984';
TYPE
TWOCHAR = STRING[2];
VAR
line :STRING[255];
num :BYTE;
count,chksum,badchk,skipped :INTEGER;
choice :CHAR;
done :BOOLEAN;
inname, outname :STRING[20];
hexfile :TEXT;
binfile :FILE of BYTE;
function mkbyte(s :TWOCHAR) :INTEGER;
VAR
t, r :INTEGER;
begin
val(concat('$', s), t, r);
mkbyte := t;
end;
procedure outnibble(n :BYTE); {write n as 1 hex character to hexfile}
begin
if n > 9 then
n := n + 7; { 10..15 -> A..F }
n := n + $30;
write(hexfile, chr(n));
end;
procedure outbyte(n :BYTE); {write n as 2 hex characters to hexfile}
VAR
t :INTEGER;
begin
outnibble((t SHR 4) AND 15);
outnibble(t AND 15);
end;
procedure CIS2bin;
VAR
i :INTEGER;
begin
clrscr;
writeln('CISBin to Binary File Converter Ver ', CBVERSION);
writeln('By Ray L. McVay - ', CBUPDATED);
writeln;
write('Input file name: ');
readln(inname);
assign(hexfile, inname);
{$I-}
reset(hexfile);
{$I+}
if ioresult <> 0 then
begin
writeln('Error: ',inname, ' not found');
halt;
end;
write('Output file name: ');
readln(outname);
assign(binfile, outname);
rewrite(binfile);
badchk := 0;
skipped := 0;
while not eof(hexfile) do
begin
readln(hexfile, line);
if line[1] = ':' then {Intel start of block character}
begin
chksum := 0;
for i := 0 to 4 do
chksum := (chksum + mkbyte(concat(line[2 * i], line[2 * i + 1])));
count := mkbyte(concat(line[2], line[3]));
for i := 0 to count - 1 do
begin
num := mkbyte(concat(line[10 + (2 * i)], line[11 + (2 * i)]));
write(binfile, num);
chksum := (chksum + num);
end;
chksum :=
(chksum +
mkbyte(concat(line[10 + 2 * count], line[11 + 2 * count])));
if (chksum AND $FF) <> 0 then
begin
write(' <== Bad Checksum (', chksum AND $FF, ')');
badchk := badchk + 1;
end;
end
else {this line was not valid data because it didn't start with :}
begin
writeln('Discarding line: ', line);
skipped := skipped + 1;
end;
end;
close(hexfile);
close(binfile);
writeln('Lines discarded: ', skipped);
writeln('Blocks with bad checksums: ', badchk);
write('Press any key to continue...');
read(kbd, choice);
end {CIS2Bin};
procedure bin2CIS;
CONST
FULLBLOCK = $18; {standard CIS record length}
VAR
oset,i,j :INTEGER;
done :BOOLEAN;
block :ARRAY[1..FULLBLOCK] of BYTE;
begin
clrscr;
writeln('Binary to CISbin File Converter Ver ', BCVERSION);
writeln('By Ray L. McVay - ', BCUPDATED);
writeln;
write('Input file name: ');
readln(inname);
assign(binfile, inname);
{$I-}
reset(binfile);
{$I+}
if ioresult <> 0 then
begin
writeln('Error: ',inname, ' not found');
halt;
end;
write('Output file name: ');
readln(outname);
assign(hexfile, outname);
rewrite(hexfile);
done := FALSE;
oset := 0;
i := 1;
repeat
{$I-}
read(binfile, block[i]);
{$I+}
if ioresult <> 0 then
begin
done := TRUE;
i := i - 1;
end;
if (i = FULLBLOCK) or done then
begin
write(hexfile, ':'); {start of block}
outbyte(i); {blocksize}
outbyte(oset SHR 8); {offset hi}
outbyte(oset AND $FF); {offset lo}
write(hexfile, '00'); {record type = data}
chksum := i + (oset AND $FF) + (oset SHR 8);
for j := 1 to i do
begin
outbyte(block[j]); {data}
chksum := chksum + block[j];
end;
outbyte($FF AND (-chksum)); {checksum}
writeln(hexfile);
oset := oset + i; {set up for next block}
i := 0;
end;
i := i + 1;
until done;
close(binfile);
close(hexfile);
end;
begin {CISbin}
done := FALSE;
repeat
clrscr;
writeln('CIS Binary ASCII File Utility - Ver ', VERSION);
writeln('Ray McVay - ', UPDATED);
writeln;
writeln('Choose an option by number:');
writeln('1. Convert a CIS ASCII binary file to DOS binary');
writeln('2. Convert a DOS binary file to CIS ASCII binary');
writeln('3. Exit to DOS');
write('Which? ');
repeat
read(kbd, choice);
until choice in ['1'..'3'];
case choice of
'1': CIS2bin;
'2': bin2CIS;
'3': done := TRUE;
end;
until done;
end.